package com.goktech.controller;

import com.goktech.entity.Admin;
import com.goktech.service.AdminService;
import com.goktech.util.javaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin/adminPCenter")

public class adminPCenterController {


    @Autowired
    private AdminService adminService;

    @Autowired
    private Admin a;








//    @RequestMapping("/show")
//    public String allPerson(HttpServletRequest request){
//        User byZhanghao = userService.findByZhanghao(u);
//        System.out.println(byZhanghao);
//        request.setAttribute("user",byZhanghao);
//        return "user/personCenter";
//
//
//
//    }


//    @PostMapping("/update")
//    @ResponseBody
//    public boolean updatePersonCenter(User u){
//        int count = 0;
//        System.out.println("u:"+u);
//        int byUser = userService.updateUser(u);
//        System.out.println("byUser:"+byUser);
//        if(byUser != 0){
//            count = userService.updateUser(u);
//            if(count != 0){
//                System.out.println("count:"+count);
//                return true;
//            }
//        }
//        return false;
//    }


    @RequestMapping("/")
    public String searchPerson2(HttpServletRequest request){
        System.out.println(111111111);
        HttpSession session = request.getSession();
        System.out.println(222222);

        String azhanghao = (String) session.getAttribute("Azhanghao");

        a.setAzhanghao(azhanghao);
        System.out.println(azhanghao);
        Admin byZhanghao = adminService.findByZhanghao(a);
        request.setAttribute("admin",byZhanghao);
        System.out.println("find7777:"+byZhanghao);
        return "admin/personCenter";



//        a.setAzhanghao(zhanghao);
//
//        System.out.println(adminService);
//        Admin byAdmin = adminService.findByAdmin(a);
//
//        if(byAdmin != null){
//            HttpSession session = request.getSession();
//            session.setAttribute("admin",byAdmin.getAdminName());
//            session.setAttribute("Azhanghao",byAdmin.getAzhanghao());

    }



    @PostMapping("/update")
    @ResponseBody
    public boolean updatePerson2(Admin a){
        int count = 0;
        System.out.println("a:"+a);
        Admin byZhanghao = adminService.findByZhanghao(a);
        if(!byZhanghao.getApassword().equals(a.getApassword())){
            a.setApassword(javaUtil.getResult(a.getApassword()));
        }
        System.out.println("byZhanghao:"+byZhanghao);
        if(byZhanghao != null){
            count = adminService.updateAdmin(a);
            if(count != 0){
                System.out.println("count:"+count);
                return true;
            }
        }
        return false;
    }


//    @PostMapping("/cancle")
//    @ResponseBody
//    public boolean canclePerson(User u){
//        System.out.println(1111111);
//        String isReturn = borrow.getIsReturn();
//        System.out.println(isReturn);
//       if(isReturn=="true"){
//           int i = userService.deleteUser(u);
//           System.out.println(i);
//           return true;
//       }
//       return false;
//
//    }


    @RequestMapping("/zhuxiao")
    public String zhuxiaoPerson2(HttpServletRequest request){

        HttpSession session = request.getSession();
        String azhanghao = (String) session.getAttribute("Azhanghao");
        a.setAzhanghao(azhanghao);
        Admin byZhanghao = adminService.findByZhanghao(a);

        System.out.println("1111111111");

        request.setAttribute("admin",byZhanghao);



        int i = adminService.deleteAdmin(byZhanghao);



        return "redirect:/login/quit";



//
//        System.out.println(2222222);
//
//        borrow.setUserId(userId);
//        borrow byBorrow = borrowService.findIsReturn(borrow);
//        System.out.println(byBorrow);
//        String isReturn = byBorrow.getIsReturn();
//        if(isReturn!=null && isReturn.equals("true")){
//            int i = userService.deleteUser(u);
//            int i1 = collectService.deleteCollect1(c);
//
//            System.out.println(i);
//            System.out.println(i1);
//            return false;
//        }
//        return false;
    }






//    @PostMapping("/update")
//    @ResponseBody
//    public boolean updatePerson(HttpServletRequest request){
//        HttpSession session = request.getSession();
//        String zhanghao = (String) session.getAttribute("zhanghao");
//        int count = 0;
//        int byUser=userService.updateUser(u);
//        request.setAttribute("user",byUser);
//        System.out.println("byUser:"+byUser);
//        if(byUser != 0){
//            count = userService.updateUser(u);
//            if(count != 0){
//                System.out.println("count:"+count);
//                return true;
//            }
//        }
//        return false;
//    }





}
